﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ReferenceManage))]
public class ReferenceCustomEditor : Editor
{
    private ReferenceManage rm;

    private void OnEnable()
    {
        rm = (ReferenceManage)target;
    }

    public override void OnInspectorGUI()
    {

        // 空格换行*2
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("热更新命名空间");
        EditorGUILayout.LabelField("热更新脚本");
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();


        rm.ButtJoinHorUpdateScript = EditorGUILayout.TextField(rm.ButtJoinHorUpdateScript);

        EditorGUILayout.EndHorizontal();


        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();

        if (GUILayout.Button("添加", GUILayout.Width(80), GUILayout.Height(25)))
        {
            rm.objectList.Add(new ReferenceData());
        }

        if (GUILayout.Button("删除", GUILayout.Width(80), GUILayout.Height(25)))
        {
            for (int i = rm.objectList.Count - 1; i >= 0; i--)
            {
                var item = rm.objectList[i];
                if (item.deleteField)
                    rm.objectList.Remove(item);
            }
        }

        if (GUILayout.Button("删除空引用", GUILayout.Width(80), GUILayout.Height(25)))
        {
            for (int i = rm.objectList.Count - 1; i >= 0; i--)
            {
                var item = rm.objectList[i];
                if (item.Object == null)
                    rm.objectList.Remove(item);
            }
        }

        if (GUILayout.Button("删除全部", GUILayout.Width(80), GUILayout.Height(25)))
        {
            for (int i = rm.objectList.Count - 1; i >= 0; i--)
            {
                var item = rm.objectList[i];
                rm.objectList.Remove(item);
            }
        }

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("选择", GUILayout.Width(50));
        EditorGUILayout.LabelField("引用变量名");
        EditorGUILayout.LabelField("引用");
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();

        for (int i = 0; i < rm.objectList.Count; i++)
        {
            var item = rm.objectList[i];

            EditorGUILayout.BeginHorizontal();
            item.deleteField = EditorGUILayout.Toggle(item.deleteField, GUILayout.Width(50));
            if (item.Object != null)
                item.name = EditorGUILayout.TextField(item.Object.name.Replace(" ", "").Replace(item.Object.name[0].ToString(), item.Object.name[0].ToString().ToLower()));
            else
                item.name = EditorGUILayout.TextField(item.name);
            item.Object = EditorGUILayout.ObjectField(item.Object, typeof(Object), true);
            if (GUILayout.Button("删除", GUILayout.Width(40), GUILayout.Height(17)))
            {
                rm.objectList.Remove(item);
            }
            EditorGUILayout.EndHorizontal();
        }
    }
}
